<template>
  <div class="wrapper">
      <el-form :inline="true">
        <el-form-item>
          <date-phase ref="datePhase" dateTypeRange="234" :datePhase="datePhase" ></date-phase>
        </el-form-item> 
        <el-form-item v-if="countryList.length > 0" style="font-weight: bold;" label="国家">
          <el-select ref="labelSelected" :filterable="true" @change="searchItem()" :clearable="true"  v-model="countryCode" placeholder="请选择国家">
            <el-option v-for="item in countryList" :key="item.code" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="searchItem()">查询</el-button>
          <el-button type="primary" @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-table ref="table" :height="tableHeight" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="160" align="center"></el-table-column>
          <el-table-column label="市场分级" prop="name" min-width="100" align="center"></el-table-column>
          <el-table-column label="整体业绩" prop="totalSale" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.totalSale? scope.row.totalSale: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="环比增长" prop="totalSaleGrowth" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.totalSaleGrowth? scope.row.totalSaleGrowth + '%': '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="平台业绩" prop="platSale" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.platSale? scope.row.platSale: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="环比增长" prop="platSaleGrowth" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.platSaleGrowth? scope.row.platSaleGrowth + '%': '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="市场业绩" prop="marketSale" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.marketSale? scope.row.marketSale: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="环比增长" prop="marketSaleGrowth" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.marketSaleGrowth? scope.row.marketSaleGrowth + '%': '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="市场折扣" prop="marketDiscount" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.marketDiscount? scope.row.marketDiscount: '0'}}
            </template>
          </el-table-column>
          <el-table-column v-if="!countryCode" label="市场广告" prop="marketAd" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.marketAd? scope.row.marketAd: '0'}}
            </template>
          </el-table-column>
          <el-table-column v-if="!countryCode" label="市场广告占比" prop="marketAdPercent" min-width="110" align="center">
            <template slot-scope="scope">
              {{scope.row.marketAdPercent? scope.row.marketAdPercent + '%': '0%'}}
            </template>
          </el-table-column>
          <el-table-column v-if="!countryCode" label="平台广告" prop="platAd" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.platAd? scope.row.platAd: '0'}}
            </template>
          </el-table-column>
          <el-table-column v-if="!countryCode" label="平台广告占比" prop="platAdPercent" min-width="110" align="center">
            <template slot-scope="scope">
              {{scope.row.platAdPercent? scope.row.platAdPercent + '%': '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="PI数" prop="piNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.piNum? scope.row.piNum: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新增客户数" prop="newCustomerNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.newCustomerNum? scope.row.newCustomerNum: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新客户业绩" prop="newCustomerSale" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.newCustomerSale? scope.row.newCustomerSale: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新客户折扣" prop="newCustomerDiscount" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.newCustomerDiscount? scope.row.newCustomerDiscount: '0'}}
            </template>
          </el-table-column>
        </el-table>
      </div>
  </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'
  import formUtil from '@/utils/formUtil'
  import DatePhase from  '@/components/sale/date-phase'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        countryData: [],
        tableData: [],
        defaultProps: {
          children: 'children',
          label: 'name',
          isLeaf: 'leaf'
        },
        datePhase:{
            dateType: 2,
            value:'',
            to:'' 
        },
        levelId: '',
        countryCode: '',
        name: '',
        countryList: [],
        tableHeight: window.tableHeight
      }
    },
    components: {
      DatePhase
    },
    mixins: [dingMixin],
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 220
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 220
      }
    },
    async mounted(){
      this.levelId = this.$route.query.levelId;
      this.countryCode = this.$route.query.countryCode;
      this.name = this.$route.query.name;
      this.datePhase.to = '';
      this.datePhase.value = '';
      await this.searchItem();
      if(this.levelId && !this.countryCode) this.countryList = await this.$httpN.postQ('mpb/market/listCountry', {level: this.levelId, query: ''}, false);
    },
    methods: {
      async handleNodeClick(node) {
        this.levelId = node.id ? node.id: '';
        this.countryCode = node.code? node.code: '';
        this.searchItem()
      },
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(this.$refs.table.$el);
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          let name = this.name ? (this.name.indexOf('市场') > 0? this.name: this.name + '市场'): '分类市场'
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${name}报表.xlsx`)
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      },
      async searchItem() {
        this.tableData = await this.$httpN.postB('mpb/report/market',{
          from: this.datePhase.value,
          to: this.datePhase.to,
          dateType: this.datePhase.dateType,
          countryCode: this.countryCode,
          levelId: this.levelId
        })
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>